package cn.edu.nuc.ssm.controller;

import cn.edu.nuc.ssm.entity.BbsSection;
import cn.edu.nuc.ssm.service.BbsSectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2017/5/9.
 */
@Controller
@RequestMapping("/section")
public class BackSectionController {

    @Autowired
    private BbsSectionService bbsSectionService;

    @RequestMapping(value = "/tolist",method = RequestMethod.GET)
    public String tolist() {

        return "back/section/list";

    }

    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public  List<BbsSection> list() {

        List<BbsSection> bbsSections = bbsSectionService.find();
        return bbsSections;
    }

    @ResponseBody
    @RequestMapping(value = "/count",method = RequestMethod.GET)
    public  String  count(int id) {

         bbsSectionService.count(id);

        return "success";
    }

    @RequestMapping(value = "/toadd",method = RequestMethod.GET)
    public String toadd(){

        return "back/section/add";
    }

    @ResponseBody
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(@RequestBody BbsSection bbsSection){


        bbsSectionService.updateSelective(bbsSection);

        return "success";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(BbsSection bbsSection, Model model){

        List<BbsSection> bbsSections = bbsSectionService.findbySname(bbsSection.getSname());

        if (bbsSections.size() > 0){

            model.addAttribute("msg","版块名已经存在了");

            return "back/section/add";
        }


        bbsSectionService.add(bbsSection);

        return "redirect:/section/tolist";
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Integer id){

        bbsSectionService.delete(id);

        return "success";
    }



}
